package com.wdd.owntool.ui.refresh

import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView.VERTICAL
import com.blankj.utilcode.util.ToastUtils
import com.wdd.libbase.Interface.RecyclerItemClickInterface
import com.wdd.libbase.base.BaseActivity
import com.wdd.owntool.R
import com.wdd.owntool.adapter.RefreshRecyclerAdapter
import kotlinx.android.synthetic.main.activity_refresh_layout.*
import java.util.*
import kotlin.collections.ArrayList


/**
 *
 * @ProjectName:    OwnTool
 * @Package:        com.wdd.owntool.ui.refresh
 * @ClassName:      SmartRefreshActivity
 * @Description:     java类作用描述
 * @Author:         王丹丹
 * @CreateDate:     2019/12/13 13:52
 * @UpdateUser:     更新者
 * @UpdateDate:     2019/12/13 13:52
 * @UpdateRemark:   更新说明
 * @Version:        1.0
 */
class SmartRefreshActivity : BaseActivity() {
    private lateinit var mAdapter: RefreshRecyclerAdapter
    var listData: MutableList<String>? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_refresh_layout
    }

    override fun initView() {
        super.initView()
        // recyclerView.isNestedScrollingEnabled = false
        recyclerView.addItemDecoration(DividerItemDecoration(this, VERTICAL))
        refreshLayout.setOnRefreshListener { layout ->
            layout.finishRefresh(2000 /*,false*/) //传入false表示刷新失败
        }
    }

    override fun initData() {
        listData = ArrayList()
        mAdapter = RefreshRecyclerAdapter(this, listData)
        recyclerView.adapter = mAdapter
        mAdapter.recyclerItemClickInterface = object : RecyclerItemClickInterface {
            override fun onItemClick(position: Int) {
                ToastUtils.showShort("点击事件")
            }
        }

        refreshLayout.setOnLoadMoreListener { refreshLayout ->
            refreshLayout.layout.postDelayed({
                //  stateFrameLayout.switchToContentState()
                val list = loadData()
                listData!!.addAll(list!!)
                mAdapter.notifyDataSetChanged()
                if (list!!.size < 10) {
                    //没有更多数据
                    refreshLayout.finishLoadMoreWithNoMoreData()
                } else {
                    refreshLayout.finishLoadMore()
                }
            }, 1000)
        }

        // stateFrameLayout.switchToEmptyState()
    }

    private val random = Random()

    private fun loadData(): Collection<String>? {
        val count: Int = 9 + random.nextInt(10)
        val list: MutableList<String> =
            ArrayList(count)
        for (i in 0 until count) {
            list.add("")
        }
        return list
    }
}